package concurrency;

import concurrency.display.NumberFrame;

public class Timer implements Runnable {

	private NumberFrame frame;
	Thread counter;
	int i;
	final static int N = 10;

	public void start() {
		frame = new NumberFrame();
		counter = new Thread(new Timer());
		i = N;
		counter.start();
	}

	public void stop() {
		counter = null;
	}

	public void run() {
		while (true) {
			if (counter == null)
				return;
			if (i >= 0) {
				tick();
				--i;
			}
		}
	}
	
	private void tick(){
	    frame.setValue(i);
	    try{ Thread.sleep(1000);}
	    catch (InterruptedException e){}
	  }
}
